#!/usr/bin/env python3
# -*- coding: utf-8 -*-
u"""
从博客中学来的10行实现的字符串模糊匹配的算法，
"""
import re
# fuzzyfinder直接从博客里拿来的代码，够用了，省一个模块的安装


def fuzzyfinder(user_input, collection):
    u"""
    thanks to @amjith: http://blog.amjith.com/fuzzyfinder-in-10-lines-of-python
    :param user_input: 
    :param collection: 
    :return: 
    """
    suggestions = []
    pattern = '.*?'.join(user_input)  # Converts 'djm' to 'd.*?j.*?m'
    for item in collection:
        # Checks if the current item matches the regex.
        match = re.search(pattern, item, re.I)
        if match:
            suggestions.append((len(match.group()), match.start(), item))

    return [x for _, _, x in sorted(suggestions)]
